/**
 * Created On: Jul 28, 2012
 */
package org.rvk.vms.utils.junit;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * A useful base class for all test cases which expect spring to instantiate dependencies.
 * 
 * The test cases need have fields and setters. They should also define IDs with the same names in the 
 * spring file. (which is typically called by the same name as the class and is in the same package)
 * For over-riding the default spring file, over-ride the getSpringConfigLocations() method.
 * <p>
 * The variables are auto-wired from Spring automatically
 * 
 * @author Ashish Kumar Sinha <aksinha.mail@gmail.com>
 * Version 1.0
 */
public class BaseSpringTestCase {
	/**
	 * Bean factory exposed sub-classes.
	 */
	protected BeanFactory beanFactory;
	
	public BaseSpringTestCase(){
		ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(getSpringConfigLocations());
		AutowireCapableBeanFactory acbf = bf.getAutowireCapableBeanFactory();
		// autowire myself.
		acbf.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
		beanFactory = bf;
	}
	
	/**
	 * Override this to use different Spring files.
	 *  
	 * Default is the same as the class under the same package structure.
	 * @return
	 */
	protected String[] getSpringConfigLocations(){
		// look for a spring file with the same name as the test class.
		String name = getClass().getName().replaceAll("\\.", "/") + ".xml";
		return new String[] {name};
	}

}
